package org.sam.beatles.ui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import org.sam.beatles.event.OpenDockWindowEvent;
import org.sam.beatles.utils.BeanUtil;
import org.sam.swing.JSPanel;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 打开窗口的操作
 * 
 * @author sam
 *
 */
public class OpenDockPanelAction extends AbstractAction implements DockPanelAttribute {

	private static final long serialVersionUID = 7353927909534347230L;

	private Class<? extends JSPanel> clz;

	private String title;

	private String group;
	
	private String position;

	private int order;

	/**
	 * 注入的bean工具助手
	 */
	@Autowired
	private BeanUtil beanUtil;

	@Override
	public Class<? extends JSPanel> getClz() {
		return clz;
	}

	@Override
	public String getTitle() {
		return title;
	}

	@Override
	public String getGroup() {
		return group;
	}

	@Override
	public int getOrder() {
		return order;
	}

	@Override
	public void setClz(Class<? extends JSPanel> clz) {
		this.clz = clz;
	}

	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public void setGroup(String group) {
		this.group = group;
	}

	@Override
	public void setOrder(int order) {
		this.order = order;
	}

	/**
	 * 打开窗口的操作,默认导航栏
	 * 
	 * @param cls 窗口类型
	 * @param title 标题
	 * @param group 分组
	 * @param order 显示次序
	 */
	public OpenDockPanelAction(Class<? extends JSPanel> cls, String title, String group, int order) {
		this(cls, title, group, order, POSITION_NAVIGATION);
	}
	
	/**
	 * 打开窗口的操作
	 * @param cls 窗口类
	 * @param title 标题
	 * @param group 分组
	 * @param order 显示次序
	 * @param postion action出现的位置
	 */
	public OpenDockPanelAction(Class<? extends JSPanel> cls, String title, String group, int order,String postion) {
		this.setClz(cls);
		this.setTitle(title);
		this.setGroup(group);
		this.setOrder(order);
		this.setPosition(postion);
		this.putValue(NAME, title);
	}

	/**
	 * 打开窗口
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			beanUtil.publishEvent(new OpenDockWindowEvent(this, this.title, this.clz));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public String toString() {
		return title;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getPosition() {
		return this.position;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(String position) {
		this.position = position;
	}

}
